package com.luck.easyfoodbatch.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luck.easyfoodbatch.App;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.adapter.ShopAdapter;
import com.luck.easyfoodbatch.adapter.ShopInfoAdapter;
import com.luck.easyfoodbatch.db.PlaceDB;
import com.luck.easyfoodbatch.entity.Place;
import com.luck.easyfoodbatch.entity.ShopInfo;
import com.luck.easyfoodbatch.mvp.home.CountryModel;
import com.luck.easyfoodbatch.mvp.home.CountryPresenter;
import com.luck.easyfoodbatch.mvp.home.CountryView;
import com.luck.easyfoodbatch.mvp.home.GoodSearchModel;
import com.luck.easyfoodbatch.mvp.home.GoodSearchPresenter;
import com.luck.easyfoodbatch.mvp.home.GoodSearchView;
import com.luck.easyfoodbatch.mvp.model.Country;
import com.luck.easyfoodbatch.mvp.model.SearchGoods;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.SharedPreferencesUtil;
import com.luck.easyfoodbatch.util.SoftKeyBoardListener;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Tools;
import com.luck.easyfoodbatch.util.Utils;
import com.luck.easyrecyclerview.EasyRecyclerView;
import com.luck.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.luck.easyrecyclerview.decoration.DividerGridItemLineDecoration;
import com.luck.easyrecyclerview.decoration.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, RecyclerArrayAdapter.OnLoadMoreListener, DrawerLayout.DrawerListener, CountryView, GoodSearchView {
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.left_back)
    ImageButton leftBack;
    @BindView(R.id.input_search_query)
    EditText inputSearchQuery;
    @BindView(R.id.ll_del)
    LinearLayout llDel;
    @BindView(R.id.searchBarContainer)
    RelativeLayout searchBarContainer;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.recyclerView)
    EasyRecyclerView recyclerView;
    @BindView(R.id.rb_sort)
    RadioButton rbSort;
    @BindView(R.id.rb_volume)
    RadioButton rbVolume;
    @BindView(R.id.rb_price)
    RadioButton rbPrice;
    @BindView(R.id.rb_day)
    RadioButton rb_day;
    @BindView(R.id.rb_screen)
    RadioButton rbScreen;
    @BindView(R.id.rgbs)
    RadioGroup rgbs;
    @BindView(R.id.drawerlayout)
    DrawerLayout drawerlayout;
    @BindView(R.id.line)
    TextView line;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.tv_positive)
    TextView tvPositive;
    @BindView(R.id.tv_advent_3)
    TextView tvAdvent3;
    @BindView(R.id.tv_advent_6)
    TextView tvAdvent6;
    @BindView(R.id.ll_right_root)
    LinearLayout ll_right_root;
    @BindView(R.id.tv_more)
    TextView tv_more;
    @BindView(R.id.btn_reset)
    Button btn_reset;
    @BindView(R.id.btn_yes)
    Button btn_yes;


    private ShopAdapter adapter;
    private List<SearchGoods.DataBean.ModelsBean> list = new ArrayList<>();
    private List<ShopInfo> infos = new ArrayList<>();
    private ShopInfoAdapter infoAdapter;
    private ShopInfo shopInfo;
    private String keywords = "", cid = "";//关键字, 类目
    private PlaceDB placeDB;
    private CountryModel countryModel;
    private List<Country.DataBean> places = new ArrayList<>();
    private List<Place> placeList = new ArrayList<>();
    private String country_id = "", nature = "", order = "";  //产地,临期,排序
    private GoodSearchModel searchModel;
    private boolean isDay = false;// 按日期
    private boolean isPrice = false;// 按价格
    private int totalPage = 1;
    private int page = 1;
    private int isRefresh = 0;// 0普通请求，1下拉刷新，2上拉加载
    private String shopUid = "";  //店铺
    private boolean isStart = false;// 是否去过更多产地页面
    private String country_type = "";  //国家类型
    private String p_id = "";  //省份
    private boolean IsFromHome = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_drawerlayout);
        App.getInstance().addActivity(ShopListActivity.this);
        ButterKnife.bind(this);
        placeDB = new PlaceDB(this);
        p_id = (String) SharedPreferencesUtil.getData(ShopListActivity.this, HttpUtil.P_ID, "3");
        searchModel = new GoodSearchPresenter(this, mContext);
        countryModel = new CountryPresenter(this, mContext);
        cid = getIntent().getStringExtra("cid");
        keywords = getIntent().getStringExtra("keywords");
        shopUid = getIntent().getStringExtra("shop_id");
        if (Tools.isNull(keywords)) {
            keywords = "";
        }
        if (Tools.isNull(cid)) {
            cid = "";
        }

        if (Tools.isNull(shopUid)) {
            shopUid = "";
        }
        country_id = getIntent().getStringExtra("country_id");// 产地id
        nature = getIntent().getStringExtra("nature");
        country_type = getIntent().getStringExtra("country_type");
        order = getIntent().getStringExtra("order");
        if (Tools.isNull(order)) {
            order = "";
        }
        if (order.equals("1")) {
            rbVolume.setTextColor(getResources().getColor(R.color.tab_color_true));
            rbSort.setTextColor(getResources().getColor(R.color.color_53));
        } else if (order.equals("2")) {
            rb_day.setTextColor(getResources().getColor(R.color.tab_color_true));
            Tools.modifyRadioButtonDrawable(this, rb_day, R.mipmap.list_scre_2x, 2);
            rbSort.setTextColor(getResources().getColor(R.color.color_53));
        }
        if (Tools.isNull(country_id)) {
            country_id = "";
        }
        if (Tools.isNull(country_type)) {
            country_type = "";
        }
        if (Tools.isNull(nature)) {
            nature = "";
        }
        countryModel.getCountryList(1);
        inputSearchQuery.setText(keywords);
        showProgressDialog(getString(R.string.app_loading));
        searchModel.searchGoods(page, p_id, cid, keywords, nature, country_id, order, shopUid, country_type);
        ViewGroup.LayoutParams params = llRight.getLayoutParams();
        params.width = (int) (App.screenWidth * 0.7);// 筛选侧滑占屏宽70%
        drawerlayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);// 设置不可触摸侧滑
        drawerlayout.addDrawerListener(this);
        inputSearchQuery.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.length() > 0) {
                    llDel.setVisibility(View.VISIBLE);
                } else {
                    llDel.setVisibility(View.GONE);
                }
            }
        });
        SoftKeyBoardListener.setListener(this, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
            @Override
            public void keyBoardShow(int height) {
                closeDrawer();
                line.setVisibility(View.VISIBLE);
            }

            @Override
            public void keyBoardHide(int height) {

            }
        });

        inputSearchQuery.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    if (!Tools.isNull(inputSearchQuery.getText().toString().trim())) {
                        keywords = inputSearchQuery.getText().toString().trim();
                        showProgressDialog("正在搜索，请稍候……");
                        list.clear();
                        isRefresh = 0;
                        page = 1;
                        searchModel.searchGoods(page, p_id, cid, keywords, nature, country_id, order, shopUid, country_type);
                        // 先隐藏键盘
                        hideKeyboard(inputSearchQuery);
                    } else {
                        T.showToast(mContext, "请输入关键字~");
                    }
                    return true;
                }
                return false;
            }
        });

        /**
         * 查询最热门的产地
         */
        new Thread(new Runnable() {
            @Override
            public void run() {
                placeList = placeDB.getByNamePlaceList("1,164,83,231,134,36,193,219,55");
                for (int i = 0; i < placeList.size() + 1; i++) {
                    shopInfo = new ShopInfo();
                    if (i == 0) {
                        // 加全部
                        shopInfo.setChecked(true);
                        shopInfo.setName("全部");
                        shopInfo.setId("");
                    } else {
                        Place place = placeList.get(i - 1);
                        shopInfo.setChecked(false);
                        shopInfo.setName(place.getName());
                        shopInfo.setId(place.getPid());
                    }
                    infos.add(shopInfo);
                }

            }
        }).start();

        GridLayoutManager manager = new GridLayoutManager(this, 2);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.addItemDecoration(new DividerGridItemLineDecoration(App.getInstance().getContext().getResources().getColor(R.color.white)));
        mRecyclerView.setLayoutManager(manager);
        infoAdapter = new ShopInfoAdapter(mContext);
        mRecyclerView.setAdapter(infoAdapter);
        infoAdapter.setList(infos);
        infoAdapter.setOnItemClickListener(new ShopInfoAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                for (ShopInfo shopInfo : infos) {
                    shopInfo.setChecked(false);
                }
                ShopInfo shopInfo = infos.get(position);
                if (shopInfo.isChecked()) {
                    shopInfo.setChecked(false);
                } else {
                    shopInfo.setChecked(true);
                    country_id = shopInfo.getId();
                }
                infoAdapter.notifyDataSetChanged();
            }
        });

        /**
         * 商品
         */
        LinearLayoutManager gridManager = new LinearLayoutManager(mContext);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.HORIZONTAL, Utils.dip2px(mContext, 0.5F), getResources().getColor(R.color.line_color)));
        recyclerView.setLayoutManager(gridManager);
        adapter = new ShopAdapter(mContext);
        recyclerView.setAdapterWithProgress(adapter);
        adapter.setMore(R.layout.view_more, this);
        adapter.setNoMore(R.layout.view_nomore);
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                SearchGoods.DataBean.ModelsBean bean = list.get(position);
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("gid", bean.getGid() + "");
                intent.putExtras(bundle);
                intent.setClass(mContext, ShopDetailsActivity.class);
                startActivityForResult(intent, 300);
            }
        });
        adapter.addAll(list);
        recyclerView.setAdapter(adapter);
        recyclerView.setRefreshListener(this);
        rbScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 按钮按下，将抽屉打开
                if (isStart) {
                    for (ShopInfo shopInfo : infos) {
                        if (shopInfo.getName().equals("全部")) {
                            shopInfo.setChecked(true);
                            country_id = shopInfo.getId();
                        } else {
                            shopInfo.setChecked(false);
                        }
                    }
                    infoAdapter.notifyDataSetChanged();
                }
                if (!Tools.isNull(nature) && nature.equals("3")) {
                    // 首页 进口或国产临期进来，不显示正期
                    tvPositive.setVisibility(View.GONE);
                } else {
                    tvPositive.setVisibility(View.VISIBLE);
                }
                hideKeyboard(inputSearchQuery);
                line.setVisibility(View.INVISIBLE);
                drawerlayout.openDrawer(Gravity.RIGHT);
                drawerlayout
                        .setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
            }
        });
    }


    @OnClick({R.id.left_back, R.id.ll_del, R.id.rb_sort, R.id.rb_volume, R.id.rb_day, R.id.rb_price, R.id.rb_screen, R.id.tv_more, R.id.btn_reset, R.id.tv_search, R.id.tv_all, R.id.tv_positive, R.id.tv_advent_3, R.id.tv_advent_6, R.id.ll_right_root, R.id.btn_yes})
    public void onClick(View view) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.left_back:
                finish();
                break;
            case R.id.ll_del:
                inputSearchQuery.setText("");
                break;
            case R.id.tv_search:
                if (!isFastDoubleClick()) {
                    if (drawerlayout.isDrawerOpen(Gravity.RIGHT)) {
                        closeDrawer();
                    }
                    if (Tools.isNull(inputSearchQuery)) {
                        T.showToast(mContext, "请输入搜索关键字~");
                    } else {
                        keywords = inputSearchQuery.getText().toString().trim();
                        showProgressDialog("正在搜索，请稍候……");
                        list.clear();
                        isRefresh = 0;
                        page = 1;
                        searchModel.searchGoods(page, p_id, cid, keywords, nature, country_id, order, shopUid, country_type);
                    }
                }
                break;
            case R.id.ll_right_root:

                break;
            case R.id.tv_more:
                // 更多产地
                isStart = true;
                intent.setClass(mContext, SelectionPointActivity.class);
                startActivityForResult(intent, 200);
                break;
            case R.id.tv_all:
                nature = "";
                tvAll.setTextColor(getResources().getColor(R.color.tab_color_true));
                tvAll.setBackgroundResource(R.drawable.btn_oval_bg);

                tvPositive.setTextColor(getResources().getColor(R.color.tab_color_false));
                tvPositive.setBackgroundResource(R.drawable.btn_oval__false_bg);

                tvAdvent3.setTextColor(getResources().getColor(R.color.tab_color_false));
                tvAdvent3.setBackgroundResource(R.drawable.btn_oval__false_bg);

                tvAdvent6.setTextColor(getResources().getColor(R.color.tab_color_false));
                tvAdvent6.setBackgroundResource(R.drawable.btn_oval__false_bg);

                break;
            case R.id.tv_positive:
                nature = "1";
                tvPositive.setTextColor(getResources().getColor(R.color.tab_color_true));
                tvPositive.setBackgroundResource(R.drawable.btn_oval_bg);

                tvAll.setTextColor(getResources().getColor(R.color.tab_color_false));
                tvAll.setBackgroundResource(R.drawable.btn_oval__false_bg);

                tvAdvent3.setTextColor(getResources().getColor(R.color.tab_color_false));
                tvAdvent3.setBackgroundResource(R.drawable.btn_oval__false_bg);

                tvAdvent6.setTextColor(getResources().getColor(R.color.tab_color_false));
                tvAdvent6.setBackgroundResource(R.drawable.btn_oval__false_bg);

                break;
            case R.id.tv_advent_3:
                nature = "2";
                tvAdvent3.setTextColor(getResources().getColor(R.color.tab_color_true));
                tvAdvent3.setBackgroundResource(R.drawable.btn_oval_bg);

                tvAll.setTextColor(getResources().getColor(R.color.tab_color_false));
                tvAll.setBackgroundResource(R.drawable.btn_oval__false_bg);

                tvPositive.setTextColor(getResources().getColor(R.color.tab_color_false));
                tvPositive.setBackgroundResource(R.drawable.btn_oval__false_bg);

                tvAdvent6.setTextColor(getResources().getColor(R.color.tab_color_false));
                tvAdvent6.setBackgroundResource(R.drawable.btn_oval__false_bg);
                break;
            case R.id.tv_advent_6:
                nature = "3";
                tvAdvent6.setTextColor(getResources().getColor(R.color.tab_color_true));
                tvAdvent6.setBackgroundResource(R.drawable.btn_oval_bg);

                tvAll.setTextColor(getResources().getColor(R.color.tab_color_false));
                tvAll.setBackgroundResource(R.drawable.btn_oval__false_bg);

                tvPositive.setTextColor(getResources().getColor(R.color.tab_color_false));
                tvPositive.setBackgroundResource(R.drawable.btn_oval__false_bg);

                tvAdvent3.setTextColor(getResources().getColor(R.color.tab_color_false));
                tvAdvent3.setBackgroundResource(R.drawable.btn_oval__false_bg);
                break;
            case R.id.btn_reset:
                tvAll.setTextColor(getResources().getColor(R.color.tab_color_true));
                tvAll.setBackgroundResource(R.drawable.btn_oval_bg);
                tvPositive.setTextColor(getResources().getColor(R.color.tab_color_false));
                tvPositive.setBackgroundResource(R.drawable.btn_oval__false_bg);
                tvAdvent3.setTextColor(getResources().getColor(R.color.tab_color_false));
                tvAdvent3.setBackgroundResource(R.drawable.btn_oval__false_bg);
                tvAdvent6.setTextColor(getResources().getColor(R.color.tab_color_false));
                tvAdvent6.setBackgroundResource(R.drawable.btn_oval__false_bg);
                for (ShopInfo info : infos) {
                    if (info.getName().equals("全部")) {
                        info.setChecked(true);
                    } else {
                        info.setChecked(false);
                    }
                }
                order = "";
                infoAdapter.notifyDataSetChanged();
                break;
            case R.id.btn_yes:

                list.clear();
                closeDrawer();
                showProgressDialog("正在搜索，请稍候……");
                isRefresh = 0;
                isStart = false;
                page = 1;
                searchModel.searchGoods(page, p_id, cid, keywords, nature, country_id, order, shopUid, country_type);
                break;
            case R.id.rb_sort:   //综合排序
                if (!isFastDoubleClick()) {
                    list.clear();
                    order = "";
                    isLoading = false;
                    rbSort.setTextColor(getResources().getColor(R.color.tab_color_true));
                    rb_day.setTextColor(getResources().getColor(R.color.color_53));
                    rbVolume.setTextColor(getResources().getColor(R.color.color_53));
                    rbPrice.setTextColor(getResources().getColor(R.color.color_53));
                    rbScreen.setTextColor(getResources().getColor(R.color.color_53));
                    loadingData();
                }
                break;
            case R.id.rb_volume:   //销量优先
                if (!isFastDoubleClick()) {
                    list.clear();
                    order = "1";
                    isLoading = false;
                    rbVolume.setTextColor(getResources().getColor(R.color.tab_color_true));
                    rbSort.setTextColor(getResources().getColor(R.color.color_53));
                    rb_day.setTextColor(getResources().getColor(R.color.color_53));
                    rbPrice.setTextColor(getResources().getColor(R.color.color_53));
                    rbScreen.setTextColor(getResources().getColor(R.color.color_53));
                    loadingData();
                }
                break;
            case R.id.rb_day:      //按日期排序
                if (!isFastDoubleClick()) {
                    list.clear();
                    isLoading = false;
                    if (!isDay) {
                        order = "2";
                        isDay = true;
                        Tools.modifyRadioButtonDrawable(this, rb_day, R.mipmap.list_scre_2x, 2);
                    } else {
                        order = "3";
                        isDay = false;
                        Tools.modifyRadioButtonDrawable(this, rb_day, R.mipmap.list_scre_1x, 2);
                    }
                    rbVolume.setTextColor(getResources().getColor(R.color.color_53));
                    rbSort.setTextColor(getResources().getColor(R.color.color_53));
                    rb_day.setTextColor(getResources().getColor(R.color.tab_color_true));
                    rbPrice.setTextColor(getResources().getColor(R.color.color_53));
                    rbScreen.setTextColor(getResources().getColor(R.color.color_53));
                    loadingData();
                }
                break;
            case R.id.rb_price:   //按价格排序
                if (!isFastDoubleClick()) {
                    list.clear();
                    isLoading = false;
                    if (!isPrice) {
                        order = "4";
                        isPrice = true;
                        Tools.modifyRadioButtonDrawable(this, rbPrice, R.mipmap.list_scre_2x, 2);
                    } else {
                        order = "5";
                        isPrice = false;
                        Tools.modifyRadioButtonDrawable(this, rbPrice, R.mipmap.list_scre_1x, 2);
                    }
                    rbPrice.setTextColor(getResources().getColor(R.color.tab_color_true));
                    rb_day.setTextColor(getResources().getColor(R.color.color_53));
                    rbSort.setTextColor(getResources().getColor(R.color.color_53));
                    rbVolume.setTextColor(getResources().getColor(R.color.color_53));
                    rbScreen.setTextColor(getResources().getColor(R.color.color_53));
                    loadingData();
                }
                break;
            case R.id.rb_screen:
                isLoading = true;
                rbPrice.setTextColor(getResources().getColor(R.color.color_53));
                rb_day.setTextColor(getResources().getColor(R.color.color_53));
                rbSort.setTextColor(getResources().getColor(R.color.color_53));
                rbVolume.setTextColor(getResources().getColor(R.color.color_53));
                rbScreen.setTextColor(getResources().getColor(R.color.tab_color_true));
                break;
        }
    }

    /**
     * 加载数据
     */
    public void loadingData() {
        if (!isLoading) {
            showProgressDialog("正在搜索，请稍候……");
            isRefresh = 0;
            page = 1;
            searchModel.searchGoods(page, p_id, cid, keywords, nature, country_id, order, shopUid, country_type);
        }
    }

    /**
     * 关闭侧滑
     */
    private void closeDrawer() {
        drawerlayout.closeDrawer(Gravity.RIGHT);
        drawerlayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    }

    @Override
    public void onLoadMore() {
        //加载
        isRefresh = 2;
        page++;
        if (!isNetworkConnected()) {
            adapter.pauseMore();
            return;
        }
        if (page > totalPage) {
            adapter.stopMore();
            return;
        }

        searchModel.searchGoods(page, p_id, cid, keywords, nature, country_id, order, shopUid, country_type);
    }

    @Override
    public void onRefresh() {
        //刷新
        isRefresh = 1;
        list.clear();
        page = 1;
        searchModel.searchGoods(page, p_id, cid, keywords, nature, country_id, order, shopUid, country_type);
    }

    private boolean isLoading = false;


    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {

    }

    @Override
    public void onDrawerOpened(View drawerView) {
    }

    @Override
    public void onDrawerClosed(View drawerView) {
        closeDrawer();
        line.setVisibility(View.VISIBLE);
    }

    @Override
    public void onDrawerStateChanged(int newState) {

    }


    @Override
    public void getCountrySuccess(Country model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                places = model.getData();
                // 保存国家到本地
                if (places != null && places.size() > 0) {
                    placeDB.savePlaceList(places);
                }
                placeList = placeDB.getByNamePlaceList("1,164,83,231,134,36,193,219,55");
                infos.clear();
                for (int i = 0; i < placeList.size() + 1; i++) {
                    shopInfo = new ShopInfo();
                    if (i == 0) {
                        // 加全部
                        shopInfo.setChecked(true);
                        shopInfo.setName("全部");
                        shopInfo.setId("");
                    } else {
                        Place place = placeList.get(i - 1);
                        shopInfo.setChecked(false);
                        shopInfo.setName(place.getName());
                        shopInfo.setId(place.getPid());
                    }
                    infos.add(shopInfo);
                }

                infoAdapter.setList(infos);
                infoAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void getGoodSearchSuccess(SearchGoods model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                SearchGoods.DataBean data = model.getData();
                if (data != null) {
                    totalPage = data.getTotalPage();
                    list.addAll(model.getData().getModels());
                } else {
                    list = new ArrayList<>();
                }
                adapter.clear();
                adapter.addAll(list);
                adapter.notifyDataSetChanged();
            } else {
                T.showToast(mContext, model.getMsg());
            }
        }
    }

    @Override
    public void httpStatusError() {

    }

    @Override
    public void hideLoading() {
        hideShowProgress();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 200:
                    if (data != null) {
                        // 更多产地 返回数据
                        if (drawerlayout.isDrawerOpen(Gravity.RIGHT)) {
                            closeDrawer();
                        }
                        String name = data.getStringExtra("name");
                        country_id = data.getStringExtra("country_id");
                        for (ShopInfo info : infos) {
                            if (info.getName().equals("全部")) {
                                info.setChecked(true);
                            } else {
                                info.setChecked(false);
                            }
                        }
                        infoAdapter.notifyDataSetChanged();
                        isRefresh = 0;
                        page = 1;
                        showProgressDialog(getString(R.string.app_loading));
                        searchModel.searchGoods(page, p_id, cid, keywords, nature, country_id, order, shopUid, country_type);
                    }
                    break;
                case 300:
                    finish();
                    break;
            }
        }
    }
}
